package com.yunwaikeji.gy_tool_library.task;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GYTaskManager {
    private static GYTaskManager taskManager;

    public static GYTaskManager getInstance() {
        if (taskManager == null) {
            synchronized (GYTaskManager.class) {
                if (taskManager == null) {
                    taskManager = new GYTaskManager();
                }
            }
        }
        return taskManager;
    }

    private List<GYAsyncTask<?, ?, ?, ?>> asyncTaskList = new ArrayList<>();

    public GYTaskManager add(List<GYAsyncTask<?, ?, ?, ?>> asyncTaskList) {
        this.asyncTaskList.addAll(asyncTaskList);
        return this;
    }

    public GYTaskManager add(GYAsyncTask<?, ?, ?, ?>... asyncTasks) {
        return add(Arrays.asList(asyncTasks));
    }

    public GYTaskManager remove(List<Integer> tagIdList) {
        for (int i = asyncTaskList.size() - 1; i >= 0; i--) {
            if (tagIdList.contains(asyncTaskList.get(i).getTagId())) {
                asyncTaskList.remove(i);
            }
        }
        return this;
    }

    public GYTaskManager remove(Integer... tagIds) {
        return remove(Arrays.asList(tagIds));
    }

    public boolean has(Integer tagId) {
        for (GYAsyncTask<?, ?, ?, ?> asyncTask : asyncTaskList) {
            if (asyncTask.getTagId().equals(tagId)) {
                return true;
            }
        }
        return false;
    }
}
